package cn.edu.swu;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

public class RegisterServlet extends HttpServlet{
    private static final long serialVersionUID = 1198763434511986380L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String user = request.getParameter("user");
        String pass = request.getParameter("pass");
        String confirm =request.getParameter("confirm");
        String phonenum =request.getParameter("phonenum");
        String code = request.getParameter("code");

        HttpSession session = request.getSession(true);
        String validateCode = (String) session.getAttribute(AuthFilter.REGISTER_VALIDATE_CODE);
        if (validateCode == null || !validateCode.equalsIgnoreCase(code)) {
            response.sendRedirect("/demo/register.html");
            return;
        }

        if (user != null && pass != null && confirm != null && phonenum != null) {
            if (user.equals("admin") && pass.equals("123456") && confirm.equals(pass) && phonenum.equals("18312345678")) {
                session.setAttribute(AuthFilter.REGISTER_STATUS, Boolean.TRUE);
                response.sendRedirect("/demo/login.jsp");
            } else {
                response.sendRedirect("/demo/register.html");
            }
        }

        
    }
}
